<template>
  <div class="flowpath-container">
    <div>
      <div class="mb20">
        <span class="color_494949 ft12 pr5">{{ translate('公司') }}：</span>
        <el-select
          v-model="formInfo.CompanyId"
          class="w164 mr14"
          :clearable="true"
          placeholder="Select"
        >
          <el-option
            v-for="item in Companylist"
            :key="item.CompanyId"
            :label="item.FullName"
            :value="item.CompanyId"
          />
        </el-select>
        <span class="color_494949 ft12 pr5">{{ translate('绑定基站') }}：</span>
        <el-select
          v-model="formInfo.BSID"
          class="w164 mr14"
          :clearable="true"
          placeholder="Select"
        >
          <el-option
            v-for="item in BaseStockData"
            :key="item.BSID"
            :label="'[' + item.BSID + ']' + item.BSName"
            :value="item.BSID"
          />
        </el-select>
        <span class="color_494949 ft12 pr5">
          {{ translate('射频码(库存)') }}：
        </span>
        <ElInput
          v-model="formInfo.ESLCode"
          class="w164 mr14"
          :placeholder="translate('请输入射频码')"
        />
        <span class="color_494949 ft12 pr5">{{ translate('出库状态') }}：</span>
        <el-select
          v-model="formInfo.StockStatus"
          class="w164 mr14"
          placeholder="Select"
        >
          <el-option
            v-for="item in StockData"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </div>
      <div class="mb20">
        <span class="color_494949 ft12 pr5">{{ translate('标签类别') }}：</span>
        <el-select
          v-model="formInfo.VerNo"
          class="w164 mr14"
          :clearable="true"
          placeholder="Select"
        >
          <el-option
            v-for="item in VerNoData"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <span class="color_494949 ft12 pr5">{{ translate('屏幕类型') }}：</span>
        <el-select
          v-model="formInfo.LCDType"
          class="w164 mr14"
          :clearable="true"
          placeholder="Select"
        >
          <el-option
            v-for="item in LCDData"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <span class="color_494949 ft12 pr5">
          {{ translate('标签标识号') }}：
        </span>
        <ElInput
          v-model="formInfo.UUID"
          class="w164 mr14"
          :placeholder="translate('请输入标签标识号')"
        />
        <span class="color_494949 ft12 pr5">
          {{ translate('条形码') }}：
        </span>
        <ElInput
          v-model="formInfo.ESLCode2"
          class="w164 mr14"
          :placeholder="translate('请输入条形码')"
        />
        <el-button class="settingBtn mr10" type="primary" @click="getList">
          {{ translate('查询') }}
        </el-button>
      </div>
      <div class="pdb10">
        <el-button class="settingBtn mr10" type="primary" @click="addESLcode('1')" plain>
          {{ translate('生成射频码') }}
        </el-button>
        <el-button class="settingBtn mr10" type="primary" @click="addESLcode('2')" plain>
          {{ translate('设置条形码') }}
        </el-button>
        <el-button class="settingBtn mr10" type="primary" @click="addESLcode('3')" plain>
          {{ translate('出库') }}
        </el-button>
        <el-button class="settingBtn mr10" type="primary" @click="batchFn" plain>
          {{ translate('批量退库') }}
        </el-button>
        <el-button class="settingBtn mr10" type="primary" @click="addFn" plain>
          {{ translate('新增') }}
        </el-button>
        <el-upload
              accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
              :action="action"
              :before-upload="beforeUpload"
              class="ml-upload"
              :headers="headers"
              name="fileInput"
              :on-error="handleError"
              :on-success="handleSuccess"
              :show-file-list="false"
              >
              <el-button type="primary" @click="uploadFn()" plain>
                      {{ translate('导入') }}
              </el-button>
        </el-upload>
      </div>
      <el-table :data="tableData" @selection-change="handleSelectionChange">
        <el-table-column prop="value" type="selection" width="55" />
        <!-- <el-table-column label="序号" type="index" width="80" /> -->
        <!-- <el-table-column label="编号" prop="ID" show-overflow-tooltip /> -->
        <el-table-column
          :label="translate('绑定基站')"
          prop="BSName"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('标签标识号')"
          prop="UUID"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('条形码')"
          prop="ESLCode2"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('MAC地址')"
          prop="ESLCode"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('密钥')"
          prop="ESLCodeOld"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('标签名称')"
          prop="ESLName"
          show-overflow-tooltip
        />
        <el-table-column :label="translate('屏幕尺寸')" show-overflow-tooltip>
          <template #default="{ row }">
            <div>
              {{ lcdtypes(row.LCDType) }}
            </div>
          </template>
        </el-table-column>
        <el-table-column :label="translate('是否低温')" show-overflow-tooltip>
          <template #default="{ row }">
            <div>
              {{ row.IsLowHeat }}
            </div>
          </template>
        </el-table-column>
        <!-- <el-table-column
          :label="translate('状态')"
          prop="InitCodeStatus"
          show-overflow-tooltip
        >
          <template #default="{ row }">
            <div :class="row.SortCode == '0' ? 'color_l' : 'color_red'">
              {{ row.SortCode === 0 ? translate('在线') : translate('离线') }}
            </div>
          </template>
        </el-table-column> -->
        <el-table-column
          :label="translate('公司ID')"
          prop="CompanyId"
          show-overflow-tooltip
        />
        <el-table-column
          :label="translate('公司名称')"
          prop="CompanyName"
          show-overflow-tooltip
        />
        <el-table-column :label="translate('类别')" show-overflow-tooltip>
          <template #default="{ row }">
            <div>
              {{
                row.VerNo === 1
                  ? translate('一代')
                  : row.VerNo === 2
                  ? translate('二代')
                  : translate('三代')
              }}
            </div>
          </template>
        </el-table-column>
        <!-- <el-table-column
          :label="translate('创建时间')"
          prop="CreateDate"
          show-overflow-tooltip
        /> -->
        <el-table-column
          :label="translate('库存状态')"
          prop="StockStatus"
          show-overflow-tooltip
        >
          <template #default="{ row }">
            <div class="status">
              {{
                row.StockStatus === 1
                  ? translate('已出库')
                  : row.StockStatus === 0
                  ? translate('未出库')
                  : translate('待定')
              }}
            </div>
          </template>
        </el-table-column>
        <el-table-column :label="translate('操作')" width="150">
          <template #default="{ row }">
            <el-button
              v-if="MenuAuths('edit')"
              plain
              size="small"
              type="primary"
              @click="detailFn(row)"
            >
              {{ translate('修改') }}
            </el-button>
            <el-button plain size="small" type="danger" @click="unbingbsFn(row)">
              {{ translate('退库') }}
            </el-button>
            <!-- <el-button
              v-if="MenuAuths('delete')"
              link
              size="small"
              type="danger"
              @click="deletedFn(row)"
            >
              写射频码
            </el-button> -->
          </template>
        </el-table-column>
      </el-table>
      <div v-if="total > 0" class="pagination">
        <ElPagination
          background
          :current-page="pageNo"
          :layout="layout"
          :page-size="pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </div>
    </div>
    <el-dialog
      v-model="dialogVisible"
      :title="title"
      :width="widthdialog"
      @close="closeFn"
      :close-on-click-modal="false"
    >
      <detail v-if="detailType === 1" :form="form" />
      <setEslCode
        v-if="detailType === 2"
        :basestockdata="BaseStockData"
        :form="setform"
      />
      <exportdetail v-if="detailType === 3" :form="exportform" />
      <editdetail
        v-if="detailType === 4 || detailType === 5"
        :basestockdata="BaseStockData"
        :form="editform"
      />
      <template #footer>
        <el-button @click="dialogVisible = false">
          {{ translate('取消') }}
        </el-button>
        <el-button type="primary" @click="submit">
          {{ translate('确认') }}
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
  import {
    Wms_ESLStockData,
    addbatcheslcode,
    editbatchuuid,
    outESLStock,
    BaseStationData,
    CompanyData,
    unbindesl,
    eslstockedit,
    ESLSave,
  } from '@/api/eslstock'
  import detail from './components/detail'
  import setEslCode from './components/setEslCode'
  import exportdetail from './components/exportdetail'
  import editdetail from './components/editdetail'
  import { useUserStore } from '@/store/modules/user'
  import { MenuAuth } from '~/src/utils/permission'
  import { translate } from '@/i18n'
  import { baseURL } from '@/config'
  const { token } = useUserStore()
  export default defineComponent({
    name: 'Arrival',
    components: { detail, setEslCode, exportdetail, editdetail },
    data() {
      return {
        translate: translate,
        tableData: [],
        searchInfo: '',
        total: 0,
        pageNo: 1,
        pageSize: 20,
        dialogVisible: false,
        layout: 'total, sizes, prev, pager, next, jumper',
        detailType: 1, // 1是生成射频码弹框 2是设置条形码弹框
        widthdialog: '600px',
        title: translate('ESL库存'),
        Companylist: [],
        BaseStockData: [],
        editform: {},
        ItemList: [],
        action: '',
        headers: '',
        exportform: {
          comid: '',
          deptid: '',
          chiledData: [
            {
              ESLCode: '',
            },
          ],
        },
        form: {
          eslCodeNum: '',
          eslCodeOld: '',
          memo: '',
          version: '',
        },
        setform: {
          bsid: '',
          lcdtype: '4.2',
          isESLCode2: 1,
          editbatchuuid: '',
          chiledData: [
            {
              UUID: '',
              ESLCode: '',
              ESLCode2: '',
              eslCodeOld: '',
            },
          ],
        },
        formInfo: {
          CompanyId: '',
          BSID: '',
          StockStatus: -1,
          ESLCode: '',
          UUID: '',
          LCDType: '-1',
          ESLCode2: '',
          VerNo: '',
        },
        StockData: [
          {
            label: translate('全部'),
            value: -1,
          },
          {
            label: translate('未出库'),
            value: 0,
          },
          {
            label: translate('已出库'),
            value: 1,
          },
        ],
        LCDData: [
          {
            label: translate('全部'),
            value: '-1',
          },
          {
             label: '300*400',
             value: '4.2',
          },
          {
             label: '296*128',
             value: '2.9',
          },
          {
             label: '250*128',
             value: '2.13',
          },
          {
             label: '480*800',
             value: '7.5',
          },
          {
             label: '296*152',
             value: '2.66',
          },
          {
             label: '152*152',
             value: '1.54',
          },
          {
             label: '480*648',
             value: '5.8',
          }
        ],
        VerNoData: [
          {
            label: translate('全部'),
            value: -1,
          },
          {
            label: translate('一代'),
            value: 0,
          },
          {
            label: translate('二代'),
            value: 1,
          },
          {
            label: translate('三代'),
            value: 2,
          },
        ],
      }
    },
    created() {
      this.MenuAuths = MenuAuth
      this.getList()
      this.CompanyData()
      this.BaseStationData()
    },
    methods: {
      lcdtypes(id) {
        switch (id) {
          case '4.2':
            return '300*400'
          case '2.9':
            return '296*128'
          case '2.66':
            return '296*152'
          case '1.54':
            return '152*152'
          case '5.8':
            return '480*648'
        }
      },
      //分页条数切换
      handleSizeChange(val) {
        this.pageSize = val
        this.getList()
      },
      //分页切换
      handleCurrentChange(val) {
        this.pageNo = val
        this.getList()
      },
      handleSelectionChange(e) {
        this.ItemList = e
      },
      async uploadFn() {
        this.headers = this.headers = {
          Authorization: `Bearer ${token}`,
        }
        this.action = `${baseURL}/Wms_ESLStock/ImportEslStock?tablename=Wms_ESLStock`
      },
      handleSuccess(data) {
        console.log(data)
        if (data.code === '200') {
          this.$message({
            type: 'success',
            message: data.message,
          })
          this.getList()
        } else {
          this.$message({
            message: data.message,
            type: 'error',
          })
        }
      },
      batchFn() {
        if (this.ItemList.length <= 0) {
          this.$message({
            type: 'warning',
            message: translate('请选择退库标签'),
          })
          return
        }
        this.saveunbindesl()
      },
      async saveunbindesl() {
        const selectData = []
        this.ItemList.map((item) => {
          selectData.push(item.ESLCode)
        })
        const params = {
          liststr: selectData.join(','),
        }
        const data = await unbindesl(params)
        if (data.code === '200') {
          this.getList()
          this.$message({
            type: 'success',
            message: data.message,
          })
        } else {
          this.$message({
            type: 'error',
            message: data.message,
          })
        }
      },
      async addESLcode(val) {
        if (val === '1') {
          this.widthdialog = '600px'
          this.title = translate('ESL库存')
          this.detailType = 1
          this.form = {
            eslCodeNum: '',
            eslCodeOld: '',
            memo: '',
            version: '',
          }
        } else if (val === '2') {
          this.widthdialog = '850px'
          this.title = translate('批量设置条形码')
          this.detailType = 2
          this.setform = {
            bsid: '',
            lcdtype: '4.2',
            isESLCode2: 1,
            editbatchuuid: '',
            chiledData:
              this.ItemList.length > 0
                ? this.ItemList
                : [
                    {
                      UUID: '',
                      ESLCode: '',
                      ESLCode2: '',
                      eslCodeOld: '',
                    },
                  ],
          }
        } else if (val === '3') {
          if(this.ItemList.length<=0) {
            this.$message({
                  type: 'warning',
                  message: translate('请选择出库的标签'),
                })
            return
          }
          this.widthdialog = '600px'
          this.title = translate('ESL库存分配给商户')
          this.detailType = 3
          this.exportform = {
            comid: '',
            deptid: '',
            chiledData: this.ItemList.length > 0
                ? this.ItemList
                : [
                    {
                      UUID: '',
                      ESLCode: '',
                      ESLCode2: '',
                      eslCodeOld: '',
                    },
                  ],
          }
        }
        this.dialogVisible = true
      },
      detailFn(row) {
        console.log(row)
        this.detailType = 4
        this.editform = row
        this.dialogVisible = true
      },
      addFn() {
        this.detailType = 5
        this.title = '新增'
        this.editform = {
          LCDType: '4.2',
          BSID: '',
          BSName:'',
          ESLCode:'',
          ESLCode2:'',
          ESLCodeOld:'',
          ESLName:'',
          UUID:'',
          VerNo:3,
          Version:'',
        }
        this.dialogVisible = true
      },
      async getList() {
        const params = {
          pageNo: this.pageNo,
          pageSize: this.pageSize,
          tableName: '',
          sort: '',
          order: '',
          wheres: JSON.stringify([
            {
              name: 'CompanyId',
              value: this.formInfo.CompanyId,
              displayType: 'like',
            },
            {
              name: 'StockStatus',
              value:
                this.formInfo.StockStatus === -1
                  ? ''
                  : this.formInfo.StockStatus,
              displayType: 'Equal',
            },
            {
              name: 'BSID',
              value: this.formInfo.BSID,
              displayType: 'like',
            },
            {
              name: 'ESLCode',
              value: this.formInfo.ESLCode,
              displayType: 'like',
            },
            {
              name: 'VerNo',
              value: this.formInfo.VerNo,
              displayType: 'like',
            },
            {
              name: 'LCDType',
              value:
                this.formInfo.LCDType === '-1' ? '' : this.formInfo.LCDType,
              displayType: 'like',
            },
            {
              name: 'UUID',
              value: this.formInfo.UUID,
              displayType: 'like',
            },
            {
              name: 'ESLCode2',
              value: this.formInfo.ESLCode2,
              displayType: 'like',
            },
          ]),
          export: false,
          value: '',
        }
        const data = await Wms_ESLStockData(params)
        this.tableData = data.data.rows
        this.total = data.data.total
        console.log(this.tableData)
      },
      async BaseStationData() {
        const params = {
          pageNo: 1,
          pageSize: 20,
          total: 0,
          tableName: '',
          sort: '',
          order: '',
          wheres: '',
          export: true,
          value: '',
        }
        const data = await BaseStationData(params)
        if (data.code === '200') {
          this.BaseStockData = data.data.rows
        }
      },
      async CompanyData() {
        const params = {
          pageNo: 0,
          pageSize: 0,
          total: 0,
          tableName: '',
          sort: '',
          order: '',
          wheres: '',
          export: true,
          value: '',
        }
        const data = await CompanyData(params)
        if (data.code === '200') {
          console.log(data)
          this.Companylist = data.data.rows
        }
      },
      async submit() {
        if (this.detailType === 1) {
          await this.addbatcheslcode()
          this.dialogVisible = false
        } else if (this.detailType === 2) {
          await this.editbatchuuid()
          this.dialogVisible = false
        } else if (this.detailType === 3) {
          if (this.exportform.chiledData) {
            let flag = true
            this.exportform.chiledData.map((item) => {
              if (!item.ESLCode) {
                flag = false
                this.$message({
                  type: 'error',
                  message: translate('请填写射频码'),
                })
                return
              }
            })
            if (flag) {
              await this.outESLStock()
            }
          }
        } else if (this.detailType === 4) {
          await this.eslstockedit()
          this.dialogVisible = false
        }else if(this.detailType ===5) {
          await this.ESLSaves()
          this.dialogVisible = false
        }
      },
      async ESLSaves() {
            const params = {
              bsid: this.editform.BSID,
              bsName: this.editform.BSName,
              uuid: this.editform.UUID,
              verNo: this.editform.VerNo,
              eslCode: this.editform.ESLCode,
              eslCode2: this.editform.ESLCode2,
              eslCodeOld: this.editform.ESLCodeOld,
              eslName: this.editform.ESLName,
              lcdType: this.editform.LCDType,
              version: this.editform.Version
            }
           const data = await ESLSave(params)
           if(data.code === '200') {
              this.$message({
              type: 'success',
              message: translate('保存成功'),
            })
            this.getList()
           }
      },
      async eslstockedit() {
        const params = this.editform
        const data = await eslstockedit(params)
        if (data.code === '200') {
          this.$message({
            type: 'success',
            message: translate('保存成功'),
          })
        }
      },
      async outESLStock() {
        const ESLarr = []
        if (
          this.exportform.chiledData &&
          this.exportform.chiledData.length > 0
        ) {
          this.exportform.chiledData.map((item) => {
            ESLarr.push(item.ESLCode)
          })
        }
        if (!this.exportform.comid) {
          this.$message({
            type: 'error',
            message: translate('请选择商户'),
          })
          return
        }
        const params = {
          liststr: ESLarr.join(','),
          comid: this.exportform.comid,
          deptid: this.exportform.deptid,
        }
        const data = await outESLStock(params)
        if (data.code === '200') {
          this.$message({
            type: 'success',
            message: translate('保存成功'),
          })
        }
        this.dialogVisible = false
      },
      async editbatchuuid() {
        const params = {
          list: this.setform.chiledData,
          bsid: this.setform.bsid,
          bsname: this.setform.bsname,
          lcdtypeid: this.setform.lcdtype,
          verno: this.setform.isESLCode2,
        }
        const data = await editbatchuuid(params)
        if (data.code === '200') {
          this.$message({
            type: 'success',
            message: translate('保存成功'),
          })
        }
      },
      async addbatcheslcode() {
        const params = {
          comID: '',
          eslCodeNum: this.form.eslCodeNum,
          eslCodeOld: this.form.eslCodeOld,
          memo: this.form.memo,
          version: this.form.version,
        }
        const data = await addbatcheslcode(params)
        if (data.code === '200') {
          this.$message({
            type: 'success',
            message: translate('保存成功'),
          })
        }
      },
      // 退库
      async unbingbsFn(row) {
        const params = {
          liststr: row.ESLCode,
        }
        const data = await unbindesl(params)
        if (data.code === '200') {
          this.getList()
          this.$message({
            type: 'success',
            message: data.message,
          })
        } else {
          this.$message({
            type: 'error',
            message: data.message,
          })
        }
      },
    },
  })
</script>
<style lang="scss" scoped>
  .flowpath-container {
    .color_494949 {
      display: inline-block;
      width: 100px;
      color: #494949;
      text-align: end;
    }
    .color_l {
      color: #42d885;
    }
    .color_red {
      color: red;
    }

    .w164 {
      width: 164px;
    }

    .mb20 {
      margin-bottom: 20px;
    }

    .mr14 {
      margin-right: 14px;
    }
    .status {
      color: #e6a23c;
    }
    .add-box {
      display: inline-block;
      // width: 100px;
      height: 30px;
      line-height: 30px;
      text-align: center;
      border: 1px solid #999;
      border-radius: 3px;
      cursor: pointer;
      padding: 0 10px;
    }
    // .add-box3 {
    //   width: 60px;
    // }
    .pdb10 {
      padding-bottom: 10px;
    }
    .ml-upload {
      display: inline-block;
      margin-left: 10px;
      vertical-align: bottom;
    }
  }
</style>
